package com.pabble.webnative;

import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

public class WebViewNative {
    private final Bridge mBridge;
    WebViewNative(Bridge br){
        mBridge = br;
    }
    @JavascriptInterface
    public String getWritePath(){
        return WebActivity.Instance.getWritePath();
    }

    @JavascriptInterface
    public String checkPermission(String list){
        return mBridge.checkPermission(list);
    }

    @JavascriptInterface
    public void applyPermission(String list){
        mBridge.applyPermission(list);
    }

    @JavascriptInterface
    public void download(String path, String appId){
        Log.i("WebViewNative","path:"+path+",aid:"+appId);
    }

    @JavascriptInterface
    public void unzip(String path, String fold){
        WebActivity.Instance.unzipFile(path,fold);
    }

    @JavascriptInterface
    public void exit(){
        WebActivity.Instance.runOnUiThread(()->{
            WebActivity.Instance.finishAndRemoveTask();
            android.os.Process.killProcess(android.os.Process.myPid());
        });
    }
    @JavascriptInterface
    public void setStorage(String key,String value){
        WebActivity.Instance.setStorage(key,value);
    }

    @JavascriptInterface
    public String getStorage(String key,String value){
        return WebActivity.Instance.getStorage(key,value);
    }

    @JavascriptInterface
    public void Toast(String context){
        Toast.makeText(WebActivity.Instance,context, Toast.LENGTH_SHORT).show();
    }

    @JavascriptInterface
    public void Alert(String title,String content,String dialogId){
        WebActivity.Instance.showAlert(title,content,dialogId);
    }

    @JavascriptInterface
    public void takePhoto(String title){
        WebActivity.Instance.runOnUiThread(()->{
            WebActivity.Instance.mBridge.takePhoto(title);
        });
    }

    @JavascriptInterface
    public void openAccelerometer(int periodUs){
        WebActivity.Instance.mBridge.openAccelerometer(periodUs);
    }
    @JavascriptInterface
    public void closeAccelerometer(){
        WebActivity.Instance.mBridge.closeAccelerometer();
    }
}
